<template>
    <div>
        <p>
            <!-- v-show="getUserResources('010101')" -->
            <button  class="btn btn-bold btn-success btn-round " v-on:click="add()">
                <i class="ace-icon fa fa-edit bigger-110"></i>
                新增
            </button>&nbsp;
            <button class="btn btn-bold btn-success btn-round " v-on:click="list(1)">
                <i class="ace-icon fa fa-refresh bigger-110"></i>
                刷新
            </button>
        </p>

        <pagination ref="pagination" v-bind:list="list" v-bind:itemCount="8"></pagination>

        <table id="simple-table" class="table  table-bordered table-hover">
            <thead>
            <tr>
                <th>管理员ID</th>
                <th>登录名</th>
                <th>昵称</th>
                <th>密码</th>
                <th>创建时间</th>
                <th>更新时间</th>
                <th >操作</th>
            </tr>
            </thead>

            <tbody>
            <tr v-for="admin in admins" :key="admin.index">
                <td>{{admin.id}}</td>
                <td>{{admin.username}}</td>
                <td>{{admin.name}}</td>
                <td>{{admin.password}}</td>
                <td>{{admin.createTime }}</td>
                <td>{{admin.updateTime }}</td>
                <td>
                    <div class="hidden-sm hidden-xs btn-group">
                        <!-- v-show="getUserResources('010103')" -->
                        <button class="btn btn-xs btn-info" v-on:click="edit(admin)">
                            <i class="ace-icon fa fa-pencil bigger-120">&nbsp;编辑用户</i>
                        </button>
                        <!-- v-show="getUserResources('010101')" -->
                        <button  class="btn btn-xs btn-success" v-on:click="editPWD(admin)">
                            <i class="ace-icon fa fa-key bigger-120">&nbsp;修改密码</i>
                        </button>
                        <!-- v-show="getUserResources('010102')" -->
                        <button  class="btn btn-xs btn-danger" v-on:click="del(admin.id)">
                            <i class="ace-icon fa fa-trash-o bigger-120">&nbsp;删除</i>
                        </button>
                    </div>

                    <div class="hidden-md hidden-lg">
                        <div class="inner pos-rel">
                            <button class="btn btn-minier btn-primary dropdown-toggle"
                                    data-toggle="dropdown" data-position="auto">
                                1
                                <i class="ace-icon fa fa-cog icon-only bigger-120"></i>
                            </button>
                            <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
                                <li>
                                    <a href="#" class="tooltip-info" data-rel="tooltip" title="view"
                                       v-on:click="edit(admin)">
                                        <span class="blue">
                                            <i class="ace-icon fa fa-pencil bigger-120"></i>
                                        </span>
                                    </a>
                                </li>
                                <li>
                                    <a href="#" class="tooltip-info" data-rel="tooltip" title="view"
                                       v-on:click="del(admin.id)">
                                        <span class="blue">
                                            <i class="ace-icon fa fa-trash bigger-120"></i>
                                        </span>
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </td>
            </tr>
            </tbody>
        </table>

        <div id="form-modal" class="modal fade" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">用户表表单</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">登录名</label>
                                <div class="col-sm-10">
                                    <!--bind事件：如果admin.id有值，=true=disabled-->
                                    <input v-bind:disabled="admin.id"
                                           v-model="admin.username"
                                           type="text" class="form-control"
                                           placeholder="登录名">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-2 control-label">昵称</label>
                                <div class="col-sm-10">
                                    <input
                                            v-model="admin.name"
                                            type="text" class="form-control"
                                            placeholder="昵称">
                                </div>
                            </div>

                            <!--如果id有值，则不显示密码编辑-->
                            <div v-show="!admin.id" class="form-group">
                                <label class="col-sm-2 control-label">密码</label>
                                <div class="col-sm-10">
                                    <input
                                            v-model="admin.password" type="password"
                                            class="form-control"
                                            placeholder="密码">
                                </div>
                            </div>

                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="button" class="btn btn-primary" v-on:click="save()">保存</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->

        <!--修改密码模态框-->
        <div id="edit-password-modal" class="modal fade" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                        <h4 class="modal-title">修改密码</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal">
                            <div class="form-group">
                                <label class="control-label col-lg-3">密码</label>
                                <div class="col-lg-9">
                                    <input class="form-control" type="password" v-model="admin.password" name="password">
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default btn-round" data-dismiss="modal">
                            <i class="ace-icon fa fa-times"></i>
                            取消</button>
                        <button type="button" class="btn btn-primary btn-round" v-on:click="savePWD()">
                            <i class="ace-icon fa fa-plus-circle"></i>
                            保存</button>
                    </div>
                </div>
            </div>
        </div>

        <!---->

    </div>
</template>

<script>
    import Pagination from "../../components/pagination";

    export default {
        name: "system-admin",
        components: {Pagination},
        data: function () {
            return {
                admin: {},
                admins: [],
            }
        },
        mounted: function () {
            let _this = this;
            _this.$refs.pagination.size = 5;
            _this.list(1);

        },
        methods:{
            list(page){
                let _this=this;
                Loading.show();
                _this.$ajax.post(process.env.VUE_APP_SERVER+'/business/admin/adminPageDTOS',
                    {
                        currentPage:page,
                        initPageNum:_this.$refs.pagination.size
                    }).then((responseDTO)=>{
                    Loading.hide();
                    console.log("查询用户列表：",responseDTO);
                    let resp = responseDTO.data;
                    _this.admins=resp.responseData.list;
                    //获取分页结果，并把总条数返回，重新渲染
                    _this.$refs.pagination.render(page,responseDTO.data.sumPage);
                })
            },
            add(){
                let _this=this;
                _this.admin={},
                $("#form-modal").modal("show");
            },
            /**
             * 点击【保存】
             */
            save() {
                let _this = this;

                //保存校验，1!=1去掉自动生成的代码第一个||或
                if (1 != 1
                    || !Validator.require(_this.admin.username, "登录名")
                    || !Validator.length(_this.admin.username, "登录名", 3, 50)
                    || !Validator.length(_this.admin.name, "昵称", 3, 50)
                    || !Validator.require(_this.admin.password, "密码")
                ) {
                    return;
                }
                //密码加密+盐值
                _this.admin.password = hex_md5(_this.admin.password + KEY);

                Loading.show();
                _this.$ajax.post(process.env.VUE_APP_SERVER + '/business/admin/adminAdd/', _this.admin)
                    .then((responseAdd) => {
                        Loading.hide();
                        console.log("保存admin表结果：", responseAdd);
                        let resp = responseAdd.data;
                        if (resp.success) {
                            $("#form-modal").modal('hide');
                            _this.list(1);
                            //添加提示
                            Toast.success("保存成功！");
                        } else {
                            console.log("走到这里了！");
                            Toast.warning(resp.responseMsg);
                        }
                    })
            },
        }
    }
</script>

<style scoped>

</style>